package com.unicom.mec.project.controller;


import com.unicom.mec.project.common.ResultBean;
import com.unicom.mec.project.remote.DeployRemote;
import com.unicom.mec.project.service.ProjectService;
import com.unicom.mec.project.vo.AddProjectVo;
import com.unicom.mec.project.vo.EditProjectVo;
import com.unicom.mec.project.vo.ProjectVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 项目控制层
 *
 * @author 刘亮武
 * @date 2020年06月12日
 */
@Api(value = "/project", tags = "项目管理")
@RestController
@RequestMapping("/project")
public class ProjectController {

    @Autowired
    private ProjectService projectService;
    @Autowired
    private DeployRemote deployRemote;

    @ApiOperation(value = "添加项目信息", httpMethod = "POST")
    @PostMapping("/")
    @ResponseBody
    ResultBean<String> addProject(@RequestBody @Valid AddProjectVo addProjectVo) {
        Long id = projectService.addProject(addProjectVo);
        ResultBean<String> result = new ResultBean<>();
        result.setData(String.valueOf(id));
        return result;
    }

    @ApiOperation(value = "修改项目信息", httpMethod = "PUT")
    @PutMapping("/")
    @ResponseBody
    ResultBean<ProjectVo> editProject(@RequestBody @Valid EditProjectVo editProjectVo) {
        ProjectVo projectVo = projectService.editProject(editProjectVo);
        // 设置统计数据 TODO 设备服务和能力服务完成后接入
        projectVo.setEdgeNodeCount(0);
        projectVo.setEdgeEndpointCount(0);
        projectVo.setApplicationCount(0);
        ResultBean<ProjectVo> result = new ResultBean<>();
        result.setData(projectVo);
        return result;
    }

    @ApiOperation(value = "查询项目信息", httpMethod = "GET")
    @GetMapping("/{id}")
    @ResponseBody
    ResultBean<ProjectVo> findProjectById(@ApiParam(value = "主键") @PathVariable(value = "id") Long id) {
        ProjectVo projectVo = projectService.findProjectById(id);
        // 设置统计数据 TODO 设备服务和能力服务完成后接入
        projectVo.setEdgeNodeCount(0);
        projectVo.setEdgeEndpointCount(0);
        projectVo.setApplicationCount(0);
        ResultBean<ProjectVo> result = new ResultBean<>();
        result.setData(projectVo);
        return result;
    }

    @ApiOperation(value = "删除项目信息", httpMethod = "DELETE")
    @DeleteMapping("/{id}")
    @ResponseBody
    ResultBean<ProjectVo> deleteProject(@ApiParam(value = "主键") @PathVariable(value = "id") Long id) {
        projectService.deleteProject(id);
        deployRemote.deleteApplicationDeployByProjectId(id);
        ResultBean<ProjectVo> result = new ResultBean<>();
        return result;
    }

    @ApiOperation(value = "查询项目信息", httpMethod = "GET")
    @GetMapping("/")
    @ResponseBody
    ResultBean<List<ProjectVo>> findProjects(@ApiParam(value = "项目名称") @RequestParam(value = "projectName", required = false) String projectName,
                                             @ApiParam(value = "租户主键") @RequestParam(value = "tenantId", required = false) Long tenantId) {
        List<ProjectVo> projectVos = projectService.findProjects(projectName, tenantId);
        // 设置统计数据 TODO 设备服务和能力服务完成后接入
        projectVos.forEach(projectVo -> {
            projectVo.setEdgeNodeCount(0);
            projectVo.setEdgeEndpointCount(0);
            projectVo.setApplicationCount(0);
        });
        ResultBean<List<ProjectVo>> result = new ResultBean<>();
        result.setData(projectVos);
        return result;
    }
}
